# -*- coding: utf-8 -*-
import tkinter as tk
from selenium import webdriver
from bs4 import BeautifulSoup
import time
import io
import sys
import re
import lxml
import xlsxwriter

str1='''
--------------------------------------------
    欢迎来到由(^v^)制作的群成员的小程序
    请根据以下注意事项爬取进行爬取：
    1、本程序暂时只支持火狐浏览器
    2、登录后请将成员表下拉到底后关闭浏览器。
    3、本程序仅供学习。
--------------------------------------------
'''
windows =tk.Tk()
windows.resizable(False, False)
CurrentShow = tk.StringVar()
CurrentShow.set(str1)
#窗口设置
windows.title("爬取群成员")
windows.geometry("300x330")

def getList(number):
    #获取输入框中的值
    n=number.get()
    if n=="":
        return
    # 爬取网页源码
    res=craw(n)
    # 处理源码数据
    if res:
        list_a = dispose(res)
    # 导出数据
    if list_a:
        write_execl(list_a, n)
        CurrentShow.set("爬取成功")
    else:
        CurrentShow.set("爬取失败")

def craw(number):
    # 打开浏览器
    url = "https://qun.qq.com/member.html#gid="+number
    driver = webdriver.Firefox()
    driver.get(url)
    # 登录需要时间
    time.sleep(5)
    # 循环加载数据
    check=False
    while(not check):
        try:
            # 滚动到网页底部
            js = "window.scrollTo(0,document.body.scrollHeight)"
            driver.execute_script(js)
            # 获取网页源码
            driver.current_url
            res = driver.page_source
        except:
            check=True
    return res

def dispose(res):
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
    soup = BeautifulSoup(res, 'lxml')
    c = soup.find_all('tr', attrs={"class": re.compile('mb')})
    list_a = []
    for i in c:
        str_a = i.text.replace('\n', '').replace('\t', ',')
        data = re.split(r',', str_a)
        data_list = [i for i in data if i != '']
        img = "https:" + i.img.get('src')
        data_list.insert(2, img)
        del data_list[0]
        if len(data_list) < 9:
            data_list.insert(2, '')
        list_a.append(data_list)
    return list_a

def write_execl(list_a, number):
    if len(list_a) > 2:
        # 创建execl
        new_time = time.strftime("_%Y%m%d%H%M%S", time.localtime())
        workbook = xlsxwriter.Workbook(number+'{}.xlsx'.format(new_time))
        worksheet = workbook.add_worksheet('sheet1')
        bold = workbook.add_format({
                            'bold': 1,  # 字体加粗
                            'fg_color': 'green',  # 单元格背景颜色
                            'align': 'center',  # 对齐方式
                            'valign': 'vcenter',  # 字体对齐方式
                            })
        work_header = ['昵称', '头像地址', '群昵称', 'QQ号', '性别', 'Q龄', '入群时间', '等级（积分）', '最后发言']
        worksheet.write_row('A1', work_header, bold)
        for index in range(len(list_a)):
            worksheet.write_row('A%s' % (2 + index), list_a[index])
        workbook.close()
    else:
        print('请检查群号是否有误，没有获取到群成员信息，放弃写入execl')

def quit():
    sys.exit()
    os._exit()

def start():
    #页面布局
    qqNumber=tk.Entry(windows)
    qqNumber.place(x=50,y=50,width=200,height=30)
    qqNumber_label=tk.Label(windows,text="请输入群号：")
    qqNumber_label.place(x=75,y=20,width=150,height=20)
    begin=tk.Button(text="开始爬取",command=lambda:getList(qqNumber))
    begin.place(x=50,y=150,width=80,height=40)
    myQuit=tk.Button(text="退出",command=lambda:quit())
    myQuit.place(x=170,y=150,width=80,height=40)
    craw_message=tk.Label(windows,textvariable=CurrentShow,bg='white',justify='left')
    craw_message.place(x=10,y=200,width=280,height=120)
    windows.mainloop()

if __name__=="__main__":
    start()